package com.ruoyi.web.controller.shop.cms;

import java.util.List;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.stereotype.Controller;
import com.ruoyi.shop.cms.domain.ShopMaterialGroup;
import com.ruoyi.common.core.controller.BaseController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.ruoyi.shop.cms.service.IShopMaterialGroupService;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 素材分组Controller
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/material/group")
public class ShopMaterialGroupController extends BaseController {
	private String prefix = "shop/cms/material/group";

	@Autowired
	private IShopMaterialGroupService shopMaterialGroupService;

	/**
	 * 新增保存素材分组
	 */
	@Log(title = "素材分组", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ShopMaterialGroup shopMaterialGroup) {
		shopMaterialGroup.setCreateBy(getLoginName());
		return toAjax(shopMaterialGroupService.insertGroup(shopMaterialGroup));
	}

	/**
	 * 修改保存素材分组
	 */
	@Log(title = "素材分组", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ShopMaterialGroup shopMaterialGroup) {
		shopMaterialGroup.setUpdateBy(getLoginName());
		return toAjax(shopMaterialGroupService.updateGroup(shopMaterialGroup));
	}

	/**
	 * 删除素材分组
	 */
	@Log(title = "素材分组", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{groupId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("groupId") Long groupId) {
		return toAjax(shopMaterialGroupService.deleteByGroupId(groupId));
	}

	/**
	 * 选择分组树
	 */
	@GetMapping(value = { "/selectGroupTree/{groupId}", "/selectGroupTree/{groupId}/{excludeId}" })
	public String selectMenuTree(@PathVariable("groupId") Long groupId,
			@PathVariable(value = "excludeId", required = false) Long excludeId, ModelMap mmap) {
		ShopMaterialGroup group = null;
		if (groupId == 0) {
			group = new ShopMaterialGroup();
			group.setGroupId(groupId);
			group.setGroupName("无");
		} else {
			group = shopMaterialGroupService.selectGroupById(groupId);
		}
		mmap.put("group", group);
		mmap.put("excludeId", excludeId);
		return prefix + "/tree";
	}

	/**
	 * 加载分组列表树（排除下级）
	 */
	@GetMapping("/treeData/{excludeId}")
	@ResponseBody
	public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId) {
		ShopMaterialGroup group = new ShopMaterialGroup();
		group.setExcludeId(excludeId);
		List<Ztree> ztrees = shopMaterialGroupService.selectGroupTreeExcludeChild(group);
		return ztrees;
	}

}
